<?php if(!defined('SPRITCH')) { exit(); }

// Spritch - An opensource PHP5 web framework

// Author: Luca Degasperi
// Licence: GNU GPL
// Site: http://www.spritch.org/

// Interface: Database Driver Interface
// Summary: This is a standard interface that all database driver must implement
// Since: 0.9.1

interface database_driver_interface 
{  
	public function __construct($info);
	public function connect($host, $username,$password);
	public function escape_value($value);
	public function fetch_one($query=false);
	public function fetch_row($query=false,$type='assoc');
	public function fetch_all($query=false,$type='assoc');	
	public function get_error();    
	public function get_insert_id();
	public function num_rows();
	public function num_affected();
	public function query($query);
	public function select_database($db);	
	public function table_exists($table);
}

// Class: Database
// Summary: This class provides a database abstraction layer
// Since: 0.9.1

class database extends spritch
{	
	private $driver;

	public function __construct($config = array())
	{
		if(empty($config))
			if(file_exists(CONFIGURATION.DS.'database'.EXT))
				include_once(CONFIGURATION.DS.'database'.EXT );
		
		if(file_exists(SYSTEM.DS.'drivers'.DS.$config['driver'].EXT))
		{
			include_once(SYSTEM.DS.'drivers'.DS.$config['driver'].EXT);
			$driver = $config['driver'].'_driver';
			$this->driver = new $driver($config);
			$this->profiler->log('connected to '.$config['name'].' database');
		}
		else
		{
			$this->profiler->warning('database driver not found');
		}
	}

	public function connect()
	{
		$connection = $this->driver->connect();
		if ($connection)	
			return true;
		else
			$this->profiler->warning('could not connect to database');
		return false;
	}

	public function escape_value($value)
	{
		return $this->driver->escape_value($value);
	}

	public function num_rows()
	{
		return $this->driver->num_rows($this->result);
	}

	public function query($query)
	{
		if($this->driver->connection == false)
			return false;
					
		$result = $this->driver->query($query);
			
		if(!$result)
		{
			$error = $this->driver->get_error();
			if($error != '')
				$this->profiler->warning('execution of query failed '.$query);
			return false;
		}
		
		$this->profiler->query($query);
				
		$query = trim(strtolower($query));
		$firstpart = substr($query,0,6);
		$insert_id = false;
		$affected = false;
		$numrows = false;
		switch($firstpart)
		{
			case 'insert': $insert_id = $this->driver->get_insert_id(); break;
			case 'delete':
			case 'replac':
			case 'update': $affected = $this->driver->num_affected(); break;
			case 'select': $numrows = $this->driver->num_rows(); break;
		}
		if(!$insert_id)
			return $insert_id;
		return true;		
	}

	public function fetch_one($query)
	{
		return $this->driver->fetch_one($query);
	}

	public function fetch_all($query=false,$type='assoc')
	{
		return $this->driver->fetch_all($query,$type);
	}

	public function fetch_row($query=false,$type='assoc')
	{
		return $this->driver->fetch_row($query,$type);
	}

	public function get_error()
	{
		return $this->driver->get_error();
	}

	public function get_queries()
	{
		return $this->driver->queries;
	}

	public function table_exists($table)
	{
		return $this->driver->table_exists($table);
	}
}
?>